package com.ruoyi.quartz.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.quartz.domain.AppRank;
import com.ruoyi.quartz.mapper.AppRankMapper;
import com.ruoyi.quartz.service.IAppRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AppRankServiceImpl implements IAppRankService {

    @Autowired
    private AppRankMapper appRankMapper;
    /**
     * 获取用户的排名
     */
    @Override
    public void getUserRank() {
        List<AppRank> userRankList = appRankMapper.getUserRankList();
        setRankData(userRankList);
        appRankMapper.delAllUserRankRow();
        appRankMapper.insetUserRank(userRankList);
    }

    /**
     * 得到部门排行榜
     */
    @Override
    public void getDeptRank() {
        List<AppRank> userRankList = appRankMapper.getDeptRankList();
        setRankData(userRankList);
        appRankMapper.delAllDeptRankRow();
        appRankMapper.insetDeptRank(userRankList);
    }

    /**
     * 获取队伍排行榜
     */
    @Override
    public void getTeamRank() {
        List<AppRank> userRankList = appRankMapper.getUserTeamRankList();
        setRankData(userRankList);
        appRankMapper.delAllTeamRankRow();
        appRankMapper.insetTeamRank(userRankList);
    }

    /**
     * 插入排名数据
     *
     * @param userRankList 结果
     */
    private void setRankData(List<AppRank> userRankList) {
        Date nowDate = DateUtils.getNowDate();
        for (int i = 0; i < userRankList.size(); i++) {
            userRankList.get(i).setCreateTime(nowDate);
            userRankList.get(i).setRowDate(nowDate);
            userRankList.get(i).setRank((long) (i + 1));
        }
    }

}
